<template>
    <div>
      <!-- 面包屑 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 卡片试图 -->
      <el-card class="box-card">
        <!-- 搜索和添加区域 -->
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input clearable @clear="getGoods" v-model="queryData.query" placeholder="请输入内容" class="input-with-select">
              <el-button @click="getGoods" slot="append" icon="el-icon-search"></el-button>
            </el-input>
          </el-col>
          <el-col :span="4">
            <el-button @click="goAddGoods()" type="primary">添加商品</el-button>
          </el-col>
        </el-row>
        <!-- 表格 -->
        <el-table :data="goods" border stripe>
          <el-table-column type="index" label="#"></el-table-column>
          <el-table-column label="商品名称" prop="goods_name"></el-table-column>
          <el-table-column label="商品价格(元)" width="120" prop="goods_price"></el-table-column>
          <el-table-column label="商品重量" width="90" prop="goods_weight"></el-table-column>
          <el-table-column label="创建时间" width="140">
            <template slot-scope="scope">
              {{ scope.row.add_time | formatDate}}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
              <el-button @click="removeGoods(scope.row.goods_id)" size="mini" type="danger" icon="el-icon-delete"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryData.pagenum"
          :page-sizes="[5, 10, 15, 40]"
          :page-size="queryData.pagesize"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-card>
    </div>
</template>

<script>
export default {
  data() {
    return {
      queryData: {
        // 查询参数
        query: '',
        // 页码
        pagenum: 1,
        // 每页条数
        pagesize: 10
      },
      goods: [],
      total: 0
    }
  },

  created() {
    this.getGoods()
  },

  methods: {
    // 获取商品列表数据
    async getGoods () {
      const { data: res } = await this.$axios.get('goods',{
        params: this.queryData
      })
      if(res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.goods = res.data.goods
      this.total = res.data.total
    },
    // 每页条数发生变化
    handleSizeChange (val) {
      this.queryData.pagesize = val 
      this.getGoods()
    },
    // 页码发生变化
    handleCurrentChange (val) {
      this.queryData.pagenum = val
      this.getGoods()
    },
    // 删除商品
    removeGoods (id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const {data: res} = await this.$axios.delete(`goods/${id}`)
        if(res.meta.status !== 200) return this.$message.error('删除失败')
        this.$message.success('删除成功')
        this.getGoods()
      }).catch(() => {
        this.$message('已取消删除');          
      });
    },
    // 跳转之添加商品
    goAddGoods () {
      this.$router.push('/goods/add')
    }
  }
};
</script>

<style scoped lang='less'>

</style>
